/*
 * AdjacencyList.cpp
 *
 *  Created on: 2012-04-04
 *      Author: tony
 */

#include "AdjacencyList.h"

namespace graph {

AdjacencyList::AdjacencyList() {
	std::cout << "    AdjacencyList constructor - no params" << std::endl;
	first = NULL;
}

AdjacencyList::AdjacencyList(const int n) {
	std::cout << "    AdjacencyList constructor: arg is " << n << std::endl;
	for (int i=0; i<n; i++)
		first = new AdjListNode(i+1, NULL, first);
}

AdjacencyList::~AdjacencyList() {
	std::cout << "    AdjacencyList destructor" << std::endl;
	while (first!=NULL){
		AdjListNode* temp = first;
		first = temp->getNextVertex();
		delete temp;
	}
}

void AdjacencyList::addEdge(const int i, const int j, const float w){
	std::cout << "     AdjacencyList addEdge - " << i << " " << j << " " << w << std::endl;
	if ( first == NULL ) {
		first = new AdjListNode( i, new DoublyLinkedList(), first );
		first = new AdjListNode( j, new DoublyLinkedList(), first );
	}
	// search for a node with i
	AdjListNode* ith = findNode( i );
	if ( ith == NULL){
		first = new AdjListNode( i, new DoublyLinkedList(), first );
		ith = first;
	}
	ith->addEdge( i, j, w );

	// search for a node with j
	AdjListNode* jth = findNode( j );
	if ( jth == NULL){
		first = new AdjListNode( j, new DoublyLinkedList(), first );
		jth = first;
	}
	jth->addEdge( j, i, w );
}

void AdjacencyList::removeEdge(const int i, const int j){
	std::cout << "     AdjacencyList removeEdge - " << i << " " << j << std::endl;
	if ( first == NULL )
		return;

	// search for a node with i
	AdjListNode* ith = findNode( i );
	ith->removeEdge( i, j );

	// search for a node with j
	AdjListNode* jth = findNode( j );
	jth->removeEdge( j, i );
}

std::vector<Vertex*>* AdjacencyList::getListOfVertices(){
	std::vector<Vertex*>* nodes = new std::vector<Vertex*>();
	AdjListNode* temp = first;
	while ( temp != NULL ){
		Vertex* v = new Vertex( temp->getVertex() );
		nodes->push_back( v );
		temp = temp->getNextVertex();
	}


	return nodes;
}

} /* namespace graph */
